const {Craw, rize, recursion_1} = require('../app')
const req = new Craw({url: 'https://lists.meizu.com/page/list?categoryid=76&click=gw_index_cdh_1'});

async function particulars() {
    await req.sleep(1500);
    await rize.click('#slProp > div:nth-child(1) > div.mod-value > div > ul > li.vm-all > a')
    await req.sleep(500);
    await rize.click('#slProp > div:nth-child(2) > div.mod-value > div > ul > li.vm-all > a')
    await req.sleep(500);
    await rize.click('#slProp > div:nth-child(3) > div.mod-value > div > ul > li.vm-all > a');
    return Promise.resolve(iphoneData())
    console.log(data)  
}


async function iphoneData() {
    let pagehref = await req.PageDown({
        fnc: function () {
            let aUrl = document.querySelectorAll('#goodsListWrap > li > a');
            return [].map.call(aUrl, (item) => {
                return item.href
            })
        }
    });
    let i = 0;
    let len = pagehref.length;
    let va = await recursion_1({
        i, len, array: pagehref, fn: async function (res, index) {
            await req.newPage({name: index, url: res})
        }
    })

    if (va) {
        let i = 0;
        let arr = []
        return new Promise(resolve => {
            (function recursion() {
                if (i >= len) {
                    arr.reverse();
                    resolve(arr);
                    return true
                }
                setTimeout(async () => {
                    i++;
                    let data = await commodityDetails();
                    arr.push(...data);
                    recursion();
                }, 1000)
            })();
        })
    }
}


async function commodityDetails() {
    console.log('=========================500毫秒开始执行========================');
    await req.sleep(500);
    let versionsOrModel = await req.evaluateWith(() => {
        let ahref = document.querySelectorAll('#property > div.property-sibling > dl > dd a');
        let arr = [].map.call(ahref, (item, index) => {
            return `#property > div.property-sibling > dl > dd > a:nth-child(${index + 1})`;
        })
        return arr
    });


    let colorClassification = await req.evaluateWith(() => {
        let ahref = document.querySelectorAll('#property > div.property-set > dl:nth-child(2) > dd > a');
        let arr = [].map.call(ahref, (item, index) => {
            return `#property>div.property-set>dl:nth-child(2)>dd>a.vm-sale-img:nth-child(${index + 1})`;
        })
        return arr
    });

    let specificationParameter = await req.evaluateWith(() => {
        let ahref = document.querySelectorAll('#detailTabFixed > div > ul > li:nth-child(2) > a');
        let arr = [].map.call(ahref, (item, index) => {
            return `#detailTabFixed > div > ul > li:nth-child(2) > a:nth-child(${index + 1})`;
        })
        return arr;
    });

    let FREQUENTLYASKEDQUESTIONS = await req.evaluateWith(() => {
        let ahref = document.querySelectorAll(' #detailTabFixed > div > ul > li:nth-child(3) > a');
        let arr = [].map.call(ahref, (item, index) => {
            return `#detailTabFixed > div > ul > li:nth-child(3) > a:nth-child(${index + 1})`;
        })
        return arr
    })

    await req.sleep(500);
    let dataArr = await req.click({
        versionsOrModel,
        colorClassification,
        specificationParameter,
        FREQUENTLYASKEDQUESTIONS,
        fnc: async function () {
            let titleNmae = document.querySelector('#property > div.property-hd > h1').innerText;
            let price = document.querySelector('#J_price').innerHTML;
            let img = document.querySelectorAll('#introduce img');
            let imgUrl = [].map.call(img, (item, index) => {
                let name = `imgUrl_${index}`;
                let obj = {};
                obj[name] = item.src;
                return obj;
            })

            return [{imgUrl}, {price}, {titleNmae}]
        },

        page: 40,

        vmmetatit: async function () {
            let ImgUrl = document.querySelectorAll('#J_previewThumb li a img');
            let arr = [].map.call(ImgUrl, (item, index) => {
                let obj = {};
                let name1 = `img_url_X_${index}`;
                let name2 = `img_url_D_${index}`;
                obj[name1] = item.src;
                obj[name2] = item.src.replace(/120x120/g, '680x680')
                return obj
            })

            return arr
        },

        specification_fn: async function () {
            let tabel = document.querySelectorAll('#standard > table > tbody tr');
            let obj = {}
            let j = '';
            let s = '';
            for (let i = 0; i < tabel.length; i++) {
                if (tabel[i].className === 'standard-table-group') {
                    let name = `particulars_${i}`;
                    let vues = 'particulars'
                    obj[name] = {
                        title: tabel[i].querySelector('th').innerText.trim()
                    }
                    obj[name][vues] = [];
                    j = name;
                    s = vues;
                    continue;
                }
                if (obj[j]) {
                    obj[j][s].push({
                        title: tabel[i].querySelector('th').innerText.trim().replace(/↵/g, ''),
                        value: tabel[i].querySelector('td').innerText.trim().replace(/↵/g, '')
                    })
                }
            }


            return obj;
        },

        FAQ_FN: async function () {
            let dd = document.querySelectorAll('#question > div > dl');
            let arr = [];
            for (let i = 0; i < dd.length; i++) {
                arr.push({
                    title: dd[i].querySelector('dt').innerText.trim(),
                    value: dd[i].querySelector('dd').innerText.trim()
                })
            }

            return arr;
        },

        written_message_fn: async function () {
            let versions = document.querySelector('#property > div.property-sibling > dl');
            let obj = []
            if (versions) {
                let title = versions.querySelector('dt').innerText.trim();
                let a = versions.querySelectorAll('dd a');
                let arr = [].map.call(a, (item, index) => {
                    let obj = {};
                    let name = `versions_${index}`;
                    obj[name] = item.innerText.trim();
                    return obj
                });
                obj.push({
                    'versionsName': {
                        'versions': title,
                        'versions_S': arr
                    }
                })
            }

            let dl = document.querySelectorAll('#property > div.property-set dl');
            for (let i=0; i < dl.length; i++) {
                let o = {};
                let name = `property_${i}`;
                o['clearfix'] = [];
                o[name] = dl[i].querySelector('dt').innerText.trim();
                let dd = dl[i].querySelectorAll('dd')
                for (let j =0;j < dd.length; j++) {
                    let name1 = `property_name_${i}`;
                    let oo = {}
                    oo[name1] = dd[j].innerText.trim()
                    o['clearfix'].push(oo)
                }
                obj.push(o)
            }
            console.log(obj)
            return obj
        },

        biaoqian_fn: async function () {
            let ahref = document.querySelectorAll('#property > div.property-set > dl:nth-child(2) > dd > a');
            let arr = [].map.call(ahref, (item, index) => {
                return `#property > div.property-set > dl:nth-child(2) > dd > a:nth-child(${index + 1})`;
            })
            return arr
        }

    })
    if (dataArr) {
        await req.closePage();
        return Promise.resolve(dataArr)
    }

}
particulars().then(arr => {

    
    console.log(arr);
})
exports.particulars = particulars;

// particulars().then(res => {
//     console.log(res.toString())
// })

